package com.example.freeze;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class RecipeDBManager{
	private static final String PACKAGE_NAME = "com.example.freeze";
    //private static final String DB_NAME = "/data/data"+PACKAGE_NAME+ "/recipe.db";
//    private static final String DB_NAME = "recipe.db";
    static final String RECIPE_DB_FILE = "/data/data/" + PACKAGE_NAME + "/recipe.db";
//    private static final int VERSION = 1;
    
//    private static final String RECIPE_ID = "recipe_id";
//    private static final String RECIPE_NAME = "recipe_name";
//    private static final String RECIPE_IMAGE = "recipe_image";
//    private static final String MEETING = "meeting";
//    private static final String TIME = "time";
   
//    private static final String TABLE_NAME = "Recipe";
//    private static final String CREATE_TABLE =
//        "CREATE TABLE " + TABLE_NAME + " (" +
//        RECIPE_ID + " integer primary key autoincrement, " +
//        RECIPE_NAME + " text not null, " +
//        RECIPE_IMAGE + " text not null )";

    //private SQLiteDatabase db = SQLiteDatabase.openDatabase(RECIPE_DB_FILE,null,SQLiteDatabase.OPEN_READWRITE);

//    public RecipeDBManager(Context context) {
//        super(context, DB_NAME, null, VERSION);
//        //db = this.getWritableDatabase();
//    }
//
//    @Override
//    public synchronized void close() {
//        db.close();
//        super.close();
//    }
//
//    @Override
//    protected void finalize() throws Throwable {
//        close();
//        super.finalize();
//    }
//
//    @Override
//    public void onCreate(SQLiteDatabase db) {
//       // db.execSQL(CREATE_TABLE);
//    }
//
//    @Override
//    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
//        onCreate(db);
//    }

    // create
//    public boolean insertInfo(String meeting, String time) {
//        ContentValues cv = new ContentValues();
//        cv.put(MEETING, meeting);
//        cv.put(TIME, time);
//        return db.insert(TABLE_NAME, null, cv) != -1;
//    }

    // read - read DB from table
    public static ArrayList<RecipeInfo> getAllInfo() { 
        ArrayList<RecipeInfo> returnList = new ArrayList<RecipeInfo>();
        SQLiteDatabase db = SQLiteDatabase.openDatabase(RECIPE_DB_FILE, null, SQLiteDatabase.OPEN_READWRITE);
        //Cursor c = db.rawQuery(TABLE_NAME, null);
        Cursor c = db.rawQuery("SELECT * FROM Recipe", null);
        
//        if (c.moveToFirst()) {
//        	RecipeInfo recipe = new RecipeInfo();
//            final int indexRecipeId = c.getColumnIndex("recipe_id");
//            final int indexRecipeName = c.getColumnIndex("recipe_name");
//            final int indexRecipeImage = c.getColumnIndex("recipe_image");
//
//            do {
//                int recipe_id = c.getInt(indexRecipeId);
//                String recipe_name = c.getString(indexRecipeName);
//                String recipe_image = c.getString(indexRecipeImage);
//                recipeInfo.add(new RecipeInfo(recipe_id, recipe_name, recipe_image));
//            } while (c.moveToNext());
//        }
        
        while(c.moveToNext()){
        	
        	RecipeInfo recipe = new RecipeInfo();
        	recipe.setRecipeId(c.getInt(c.getColumnIndex("recipe_id")));
        	recipe.setRecipeName(c.getString(c.getColumnIndex("recipe_name")));
        	recipe.setRecipeImage(c.getString(c.getColumnIndex("recipe_image")));
        	
        	returnList.add(recipe);
        }
        c.close();
        db.close();

        return returnList;
    }
//
//    // update
//    public boolean updateInfo(RecipeInfo i) {
//        ContentValues cv = new ContentValues();
//        cv.put(RECIPE_NAME, i.getRecipeName());
//        cv.put(RECIPE_IMAGE, i.getRecipeImage());
//        String[] params = new String[] { Integer.toString(i.getRecipeId()) };
//        int result = db.update(TABLE_NAME, cv, RECIPE_ID + "=?", params);
//        return result > 0;
//    }
//
//    // delete
//    public boolean deleteInfo(int id) {
//        String[] params = new String[] { Integer.toString(id) };
//        int result = db.delete(TABLE_NAME, RECIPE_ID + "=?", params);
//        return result > 0;
//    }
//    
//    public boolean deleteAll() {
//        int result=db.delete(TABLE_NAME, null, null);
//        return result > 0;
//     }
}
